package com.ly.matting.controller;

import com.ly.matting.model.PortraitMattingService;
import com.ly.utils.ImageConvertUtils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api")
public class PortraitMattingController {


    @Autowired
    private PortraitMattingService portraitMattingService;

    /**
     * 处理来自前端的图像抠图请求。
     *
     * @param originalImageFile 原始图像文件
     * @param b
     * @param g
     * @param r
     * @return 返回处理后的图像
     */
    @PostMapping("/portraitMattingBGR")
    public ResponseEntity<byte[]> matPortrait(@RequestParam("originalImage") MultipartFile originalImageFile, @RequestParam("b") Integer b, @RequestParam("g") Integer g, @RequestParam("r") Integer r) {
        Mat originalImage = null;
        Mat resultMat = null;
        Mat backgroundImage = null;
        double w = 0;
        double h = 0;
        try {
            // 假设有一个服务来处理文件转Mat的过程
            originalImage = ImageConvertUtils.fileToMat(originalImageFile);
            if (originalImage == null) {
                throw new RuntimeException("上传图片不存在，请检查，如有疑问请联系作者：sulv0302");
            }
            h = originalImage.rows();
            w = originalImage.cols();
            backgroundImage = new Mat(originalImage.rows(), originalImage.cols(), CvType.CV_8UC3, new Scalar(b, g, r));
            Imgproc.resize(originalImage, originalImage, new Size(1920, 1080));
            Imgproc.resize(backgroundImage, backgroundImage, new Size(1920, 1080));
            resultMat = portraitMattingService.portraitMatting(originalImage, backgroundImage, new Size(w, h));
            byte[] imageBytes = ImageConvertUtils.matToByteArray(resultMat);
            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_JPEG);
            headers.setContentLength(imageBytes.length);
            // 返回图片数据
            return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (originalImage != null) originalImage.release();
            if (backgroundImage != null) backgroundImage.release();
            if (resultMat != null) resultMat.release();
        }
    }


    /**
     * 处理来自前端的图像抠图请求。
     *
     * @param originalImageFile 原始图像文件
     * @param backgroundFile    背景图像文件
     * @return 返回处理后的图像
     */
    @PostMapping("/portraitMatting")
    public ResponseEntity<byte[]> matPortrait(@RequestParam("originalImage") MultipartFile originalImageFile, @RequestParam("backgroundImage") MultipartFile backgroundFile) {
        Mat originalImage = null;
        Mat backgroundImage = null;
        Mat resultMat = null;
        double w = 0;
        double h = 0;
        try {
            // 假设有一个服务来处理文件转Mat的过程
            originalImage = ImageConvertUtils.fileToMat(originalImageFile);
            backgroundImage = ImageConvertUtils.fileToMat(backgroundFile);
            w = originalImage.cols();
            h = originalImage.rows();
            Imgproc.resize(originalImage, originalImage, new Size(1920, 1080));
            Imgproc.resize(backgroundImage, backgroundImage, new Size(1920, 1080));
            resultMat = portraitMattingService.portraitMatting(originalImage, backgroundImage, new Size(w, h));
            byte[] imageBytes = ImageConvertUtils.matToByteArray(resultMat);
            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_JPEG);
            headers.setContentLength(imageBytes.length);
            // 返回图片数据
            return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (originalImage != null) originalImage.release();
            if (backgroundImage != null) backgroundImage.release();
            if (resultMat != null) resultMat.release();
        }
    }


}
